<template>
  <el-dialog title="电脑设备信息添加" :visible.sync="dialog_visible">
    <div class="form-padding">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        auto-complete="on"
        label-width="90px"
        style="width: 100%"
      >
        <el-form-item label="序号" prop="id">
          <el-input
            disabled
            style="width: 100%"
            placeholder="新增无需序号"
            v-model="temp.id"
          />
        </el-form-item>
        <el-form-item label="资产类型" prop="asset_type">
          <el-input
            disabled
            placeholder="请输入资产类型"
            style="width: 100%"
            v-model="temp.asset_type"
          />
        </el-form-item>
        <el-form-item label="设备号" prop="device_no">
          <el-input
            placeholder="请输入设备号"
            style="width: 100%"
            v-model="temp.device_no"
          />
        </el-form-item>
        <el-form-item label="品牌型号" prop="brand_model">
          <el-input
            placeholder="请输入品牌型号"
            style="width: 100%"
            v-model="temp.brand_model"
          />
        </el-form-item>
        <el-form-item label="价值" prop="worth">
          <el-input
            placeholder="请输入价值"
            style="width: 100%"
            v-model="temp.worth"
          />
        </el-form-item>
        <el-form-item label="使用人" prop="user">
          <el-input
            placeholder="请输入使用人"
            style="width: 100%"
            v-model="temp.user"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            placeholder="请输入备注"
            style="width: 100%"
            v-model="temp.remark"
          />
        </el-form-item>       
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { notExistAdd,isexist_num } from "@/api/adm_device";
import g from "@/utils/utilFn";
export default {
  name: "deviceAdd",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    const validaterole_name = (rule, value, callback) => {
      if (value.length > 30) {
        callback(new Error("名称不能太长！"));
      } else {
        callback();
      }
    };
    return {
      temp: {
        id: "",
        asset_type: "电脑设备",
        device_no: "",
        brand_model: "",
        worth: "",
        user: "",
        remark: "",   
      },
      rules: {
        asset_type: [
          { required: true, message: "资产类型不能为空", trigger: "blur" },
          { required: true, trigger: "blur", validator: validaterole_name },
        ],
        device_no: [
          { required: true, message: "设备型号不能为空", trigger: "blur" },
        ],
        brand_model: [
          { required: true, message: "品牌型号不能为空", trigger: "blur" },
          { required: true, trigger: "blur", validator: validaterole_name },
        ]
      },
    };
  },
  computed: {
    dialog_visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit("update:show", val);
      },
    },
  },
  created() {},
  watch: {
    dialog_visible(newValue, oldValue) {
      if (newValue) {
      }
    },
  },
  methods: {
    cancel() {
      this.dialog_visible = false;
    },
    submit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          const param = {
            asset_type: this.temp.asset_type,
            device_no: this.temp.device_no,
            brand_model: this.temp.brand_model,
            worth: this.temp.worth,
            user: this.temp.user,
            remark: this.temp.remark,        
          };
          notExistAdd(param).then((response) => {
            if (response.data > 0) {
              setTimeout(() => {
                this.dialog_visible = false;
                this.$message({
                  type: "success",
                  message: "电脑设备信息添加成功!",
                });
                this.$refs["dataForm"].resetFields();
                this.$emit("success", param);
              }, 200);
            } else {
              this.$message({
                type: "info",
                message: "添加失败，设备号重复",
              });
            }
          });
        }
      });
    },
  },
};
</script>